package com.koron.ticket.fake.operation;

import cn.hutool.core.convert.Convert;
import com.koron.bean.system.staff.vo.EamUser;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.common.core.util.CodeTools;
import com.koron.common.core.util.StringUtils;
import com.koron.ticket.fake.operation.bean.OperationFormworkBean;
import com.koron.ticket.fake.operation.bean.OperationFormworkDto;
import com.koron.ticket.fake.operation.bean.OperationFormworkStepBean;
import com.koron.ticket.fake.operation.mapper.OperationFormworkMapper;
import com.koron.ticket.fake.operation.mapper.OperationFormworkStepMapper;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import static com.koron.common.core.util.StringUtils.isNotEmpty;

/**
 * @Author: rocky
 * @Date: 2022/1/5 14:38
 */
@Service
public class OperationFakeService {

    private EamUser getCurrUser() {
        return ThreadLocalContext.get();
    }


    @TaskAnnotation("fakeSave")
    public synchronized String fakeSave(SessionFactory factory, OperationFormworkDto dto) {
        OperationFormworkMapper mapper = factory.getMapper(OperationFormworkMapper.class);//操作票-样票主表
        OperationFormworkStepMapper stepmapper = factory.getMapper(OperationFormworkStepMapper.class);//操作步骤
        String id = CodeTools.getCode32();

        OperationFormworkBean bean = Convert.convert(OperationFormworkBean.class, dto);

        if (StringUtils.isEmpty(bean.getId())) {
            bean.setId(id);
            mapper.insertSelective(bean);
            generateDet(bean, stepmapper, id);//生成 操作步骤明细
        } else {
            mapper.updateByPrimaryKeySelective(bean);
            stepmapper.deleteByOperationId(bean.getId());
            generateDet(bean, stepmapper, bean.getId());//生成 安全措施明细、操作步骤明细
        }

        return bean.getId();
    }


    @TaskAnnotation("details")
    public OperationFormworkDto details(SessionFactory factory, String id) {

        OperationFormworkMapper mapper = factory.getMapper(OperationFormworkMapper.class);//样票主表
        List<OperationFormworkStepBean> steptemlist = factory.getMapper(OperationFormworkStepMapper.class).queryByOperationId(id);//样票详情

        OperationFormworkBean bean = mapper.selectByPrimaryKey(id);
        OperationFormworkDto dto = Convert.convert(OperationFormworkDto.class, bean);


        if (StringUtils.isNotEmpty(steptemlist)) {
            List<OperationFormworkStepBean> steplist = steptemlist.stream().filter(st -> StringUtils.isEmpty(st.getParentId())).collect(Collectors.toList());

            steplist.stream().forEach(st -> {
                List<OperationFormworkStepBean> children = steptemlist.stream().filter(tem -> st.getId().equals(tem.getParentId())).collect(Collectors.toList());
                st.setChildren(children);
            });

            dto.setStepdet(steplist);
        }
        return dto;
    }

    /**
     * 保存操作步骤
     *
     * @param bean
     * @param stepmapper
     * @param id
     */
    private void generateDet(OperationFormworkBean bean, OperationFormworkStepMapper stepmapper, String id) {
        //操作步骤明细
        if (isNotEmpty(bean.getStepdet())) {
            bean.getStepdet().stream().forEach(b -> {
                String steid = CodeTools.getCode32();
                b.setId(steid);
                b.setOperationId(id);
                //子步骤不为空
                if (StringUtils.isNotEmpty(b.getChildren())) {
                    b.getChildren().stream().forEach(d -> {
                        d.setParentId(steid);
                        d.setId(CodeTools.getCode32());
                        d.setOperationId(id);
                    });
                    stepmapper.batchInsert(b.getChildren());//写入子步骤
                }
            });
            stepmapper.batchInsert(bean.getStepdet());//写入主步骤
        }
    }
}
